package com.zxy.ziems.server.log.service.impl;

import com.zxy.btp.common.response.PageResult;
import com.zxy.btp.security.service.UserService;
import com.zxy.ziems.server.foms.dto.UserInfo;
import com.zxy.ziems.server.foms.service.FomsRpcService;
import com.zxy.ziems.server.log.DataRecordTypeEnum;
import com.zxy.ziems.server.log.mapper.DataRecordMapper;
import com.zxy.ziems.server.log.pojo.entity.DataRecordEntity;
import com.zxy.ziems.server.log.pojo.entity.DataRecordLogEntity;
import com.zxy.ziems.server.log.pojo.param.DataRecordParam;
import com.zxy.ziems.server.log.pojo.vo.DataRecordVO;
import com.zxy.ziems.server.log.service.DataRecordLogService;
import com.zxy.ziems.server.log.service.DataRecordService;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import com.zxy.ziems.server.utils.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DataRecordServiceImpl extends ServiceImplX<DataRecordMapper, DataRecordEntity> implements DataRecordService {

    @Autowired
    private DataRecordLogService dataRecordLogService;
    @Autowired
    private FomsRpcService fomsRpcService;

    @Transactional(propagation = Propagation.MANDATORY)
    @Override
    public void add(DataRecordTypeEnum dataRecordTypeEnum, String tag, String operId, List<DataRecordLogEntity> logs) {
        if (CollectionUtils.isEmpty(logs)) return;
        AssertUtil.isFalse(CollectionUtils.isEmpty(logs), "记录不可为空");
        DataRecordEntity entity = new DataRecordEntity();
        entity.setTag(tag);
        entity.setOperName(dataRecordTypeEnum.getName());
        entity.setOperType(dataRecordTypeEnum.getCode());
        entity.setOperId(operId);
        save(entity);
        logs.forEach(m -> m.setDataRecordId(entity.getId()));
        List<DataRecordLogEntity> collect = logs.stream().filter(m -> m.getOldData() != null && m.getNewData() != null
                && !m.getOldData().equals(m.getNewData())).collect(Collectors.toList());
        dataRecordLogService.adds(collect);
    }

    @Override
    public PageResult<DataRecordVO> page(DataRecordParam param) {
        PageResult<DataRecordVO> dataRecordVOPageResult = customPage(param, (page, param1) -> baseMapper.page(page, param1));
        if (dataRecordVOPageResult.getTotal() > 0) {
            Set<String> userId = dataRecordVOPageResult.getList().stream().map(DataRecordVO::getCreateUserId).collect(Collectors.toSet());
            List<UserInfo> userInfos = fomsRpcService.batchDetail(userId);
            Map<String, String> userMap = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, UserInfo::getName));
            for (DataRecordVO dataRecordVO : dataRecordVOPageResult.getList()) {
                dataRecordVO.setCreateUserName(userMap.get(dataRecordVO.getCreateUserId()));
            }
        }
        return dataRecordVOPageResult;
    }
}
